package com.wecan.components.pay.service;

import com.alibaba.fastjson.JSONObject;
import com.wecan.api.cim.MessageApi;
import com.wecan.api.saasdata.OrderData;
import com.wecan.base.BO.SystemMessageBO;
import com.wecan.base.BO.SystemMessageContentBO;
import com.wecan.base.PO.Order;
import com.wecan.base.constants.RedisKey;
import com.wecan.base.enums.*;
import com.wecan.base.utils.TemplateUtils;
import com.wecan.cache.utils.RedisUtil;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * @Author liuzhihao
 * @Date 2022.02.25
 * @Version ${version}
 * @Description ${description}
 */
public class ProcessOrderService {

    @Autowired
    private OrderData orderData;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MessageApi messageApi;

    /**
     * 处理微信订单
     *
     * @param data
     */
    public boolean processWxOrder(JSONObject data) {
        // 处理订单业务逻辑
        String outTradeNo = data.getString("out_trade_no");
        String tradeState = data.getString("trade_state");
        long orderId = NumberUtils.toLong(outTradeNo);
        Order order = orderData.queryById(orderId);
        if (order == null) {
            return true;
        }
        ItemType type = order.getType();
        if (type == ItemType.KNOWLEDGE) {
            return processContentOrder(order, tradeState);
        } else if (type == ItemType.GOOD) {
            return processGoodOrder(order, tradeState);
        }
        return true;
    }

    /**
     * 处理内容订单
     * @param order
     */
    private boolean processContentOrder(Order order, String orderState) {
        Order updateOrder = new Order();
        updateOrder.setId(order.getId());
        String redisKey = RedisKey.WX_PAY_ORDER + order.getId();
        if ("SUCCESS".equalsIgnoreCase(orderState)) {
            // 支付成功
            redisUtil.del(redisKey);
            updateOrder.setStatus(OrderStatus.HAS_DONE);
        } else if ("CLOSED".equalsIgnoreCase(orderState)) {
            // 支付关闭
            redisUtil.del(redisKey);
            updateOrder.setStatus(OrderStatus.HAS_CLOSE);
        }
        return orderData.updateOrderStatus(updateOrder);
    }

    /**
     * 商品订单处理逻辑
     * @param order
     * @param orderState
     */
    private boolean processGoodOrder(Order order, String orderState) {
        return true;
    }

    /**
     * 处理退款
     * @param jsonObject
     * @return
     */
    public boolean processRefundOrder(JSONObject jsonObject) {
        Long orderId = NumberUtils.toLong(jsonObject.getString("out_trade_no"));
        Order order = orderData.queryById(orderId);
        if (order == null) {
            return true;
        }
        Order order1 = new Order();
        order1.setId(orderId);
        order1.setStatus(OrderStatus.HAS_REFUND);
        boolean result = orderData.updateOrderStatus(order1);
        if (result) {
            // 向用户发送通知
            int fee = order.getFinalFee().multiply(new BigDecimal(100)).intValue();
            int refundFee = fee / 2;
            double money = refundFee / 100.0;
            SystemMessageBO<SystemMessageContentBO> systemMessage = new SystemMessageBO<>();
            systemMessage.setType(SystemMessageType.ORDER);
            systemMessage.setContentType(MessageContentType.ORDER);
            systemMessage.setToUserId(order.getUserId());
            systemMessage.setMessageType(MessageType.SYSTEM);
            systemMessage.setContent(TemplateUtils.refundSuccess(orderId, money));
            messageApi.sendSystemMessage(systemMessage);
        }
        return result;
    }
}
